﻿using FinalYear.OpticalFlow.Classes;
using FinalYear.OpticalFlow.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace FinalYear.OpticalFlow
{
    public partial class iNavigate : Telerik.WinControls.UI.RadForm
    {
        private Navigator m_Model;

        public iNavigate(Navigator mainModel)
        {
            InitializeComponent();
            m_ImagesSplitter.SplitterDistance = m_ImagesSplitter.Width / 2;
            m_Model = mainModel;

            UpdateFromModel();
            Application.Idle += ProcessFrame;
            m_Model.Changed += new EventHandler(OnModelChanged);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            // If the MainForm is not minimized, save the current Size and
            // location to the settings file.  Otherwise, the existing values
            // in the settings file will not change...
            if (this.WindowState != FormWindowState.Minimized)
            {
                Settings.Default.Size = this.Size;
                Settings.Default.Location = this.Location;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Size formSize = Settings.Default.Size;
            if (formSize.Height != 0)
            {
                this.Size = formSize;
                this.Location = Settings.Default.Location;
            }
        }
        private void m_MinCollisionMagnitude_KeyUp(object sender, KeyEventArgs e)
        {
            m_ApplyButton.PerformClick();
        }

        private void m_SonarCheckBox_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args)
        {
            RadCheckBox checkBox = (RadCheckBox)sender;
            m_Model.UseSonar = checkBox.Checked;
        }

        /// <summary>
        /// Called when [apply button clicked].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void OnApplyButtonClicked(object sender, EventArgs e)
        {
            int maxFeatureCount;
            if (Int32.TryParse(m_MaxFeatureCountTextBox.Text, out maxFeatureCount))
            {
                m_Model.MaxFeatureCount = maxFeatureCount;
            }

            int blockSize;
            if (Int32.TryParse(m_BlockSizeTextBox.Text, out blockSize))
            {
                m_Model.BlockSize = blockSize;
            }

            double qualityLevel;
            if (Double.TryParse(m_QaulityLevelTextBox.Text, out qualityLevel))
            {
                m_Model.QualityLevel = qualityLevel;
            }

            double minDistance;
            if (Double.TryParse(m_MinDistanceTextBox.Text, out minDistance))
            {
                m_Model.MinDistance = minDistance;
            }

            double kMean;
            if (Double.TryParse(m_KMeanTestBox.Text, out kMean))
            {
                m_Model.KMean = kMean;
            }

            int minCollisionMagnitude;
            if (int.TryParse(m_MinCollisionMagnitude.Text, out minCollisionMagnitude))
            {
                m_Model.MinimumCollisionMagnitude = minCollisionMagnitude;
            }
        }

        private void OnModelChanged(object sender, EventArgs e)
        {
            UpdateFromModel();
        }

        /// <summary>
        /// Processes the frame.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void ProcessFrame(object sender, EventArgs e)
        {
            m_Model.ProcessFrame();
            m_ImageBoxWithHeading.ImageBox.Image = m_Model.CurrentImage;
            m_FoundFeaturesCountTextBox.Text = m_Model.CurrentFoundFeaturesCount.ToString();
            m_TrackedFeaturesCountTextBox.Text = m_Model.TrackedFeaturesCount.ToString();
            m_NotTrackedFeaturesCount.Text = m_Model.NotTrackedFeaturesCount.ToString();
            m_CenterCollision.Text = m_Model.OpticalFlowStoreData.totalCollisionTime.ToString();
            m_FlowImageBoxWithHeading.ImageBox.Image = m_Model.FlowImage;

            // check if either collision area is > 0
            if (m_Model.OpticalFlowStoreData.left.sumGroundCountX > 0)
            {
                m_LeftFlow.Text = ((double)m_Model.OpticalFlowStoreData.left.sumGroundCountX / (double)m_Model.OpticalFlowStoreData.left.groundCount).ToString();
            }

            if (m_Model.OpticalFlowStoreData.right.sumGroundCountX > 0)
            {
                m_RightFlow.Text = ((double)m_Model.OpticalFlowStoreData.right.sumGroundCountX / (double)m_Model.OpticalFlowStoreData.right.groundCount).ToString();
            }
            if (!string.IsNullOrEmpty(m_Model.TranslateDirection))
            {
                m_TurnDirection.Text = m_Model.TranslateDirection;
            }

            m_TurnDirection.Text = m_Model.TranslateDirection;//.ToString();
            m_SonarDistance.Text = m_Model.CurrentCollisionHistogram.SonarCollisionDistance.ToString();
        }

        private void UpdateFromModel()
        {
            m_MaxFeatureCountTextBox.Text = m_Model.MaxFeatureCount.ToString();
            m_BlockSizeTextBox.Text = m_Model.BlockSize.ToString();
            m_QaulityLevelTextBox.Text = m_Model.QualityLevel.ToString();
            m_MinDistanceTextBox.Text = m_Model.MinDistance.ToString();
            m_MinCollisionMagnitude.Text = m_Model.MinimumCollisionMagnitude.ToString();
        }
    }
}